package com.hn.controller;

import com.alibaba.fastjson.JSONArray;
import com.hn.pojo.BdMap;
import com.hn.service.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @作者 : 王昌典
 * @创建时间 : 2019/5/15 - 1:31
 */
@Controller
@RequestMapping("map")
public class bdMapCoutroller {

    @Autowired
    StudentService studentService;
    @Autowired
    GuanXiService guanXiService;
    @Autowired
    CourseService courseService;
    @Autowired
    BanjiService banjiService;
    @Autowired
    SCService scService;
    @Autowired
    BdMapService bdMapService;

    //    保存覆盖物
    @ResponseBody
    @PostMapping(value = "saveCover.html", produces = "application/json;charset=utf-8")
    public String saveCover(@RequestBody String Cover,String color, String name,String area,String state, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        System.err.println("原始数据" + Cover);
        System.err.println("转为json后Cover" + JSONObject.fromObject(Cover).getString("Cover"));

        JSONObject data = JSONObject.fromObject(Cover);
        //获得系统实时时间(索引)
        long index = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        BdMap bdMap = new BdMap();
        bdMap.setCoveroord(JSONObject.fromObject(Cover).getString("Cover"));
        bdMap.setArea(Double.valueOf(data.getString("area")));
        bdMap.setName(data.getString("name"));
        bdMap.setState(Integer.parseInt(data.getString("state")));
        bdMap.setColor(data.getString("color"));
        bdMap.setZindex(index);
       // 再次调取出当前这个覆盖物并返回
        BdMap overlay = bdMapService.findCoverByIndex(JSONObject.fromObject(Cover).getString("Cover"));
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        com.alibaba.fastjson.JSONObject retdata = new com.alibaba.fastjson.JSONObject();


        if (overlay!=null){
            //条件，已存在的就修改
            BdMap bdMap1 = new BdMap();
            bdMap1.setCoveroord(JSONObject.fromObject(Cover).getString("Cover"));  //此项必须设置，这是修改条件
            bdMap1.setArea(Double.valueOf(data.getString("area")));
            bdMap1.setName(data.getString("name"));
            bdMap1.setState(Integer.parseInt(data.getString("state")));
            bdMapService.editCoverByIndex(bdMap1);
            BdMap overlay1 = bdMapService.findCoverByIndex(JSONObject.fromObject(Cover).getString("Cover"));
            jsonObject.put("mapCover", JSONArray.parseArray(overlay1.getCoveroord()));
            jsonObject.put("zindex", overlay1.getZindex());
            jsonObject.put("name", overlay1.getName());
            jsonObject.put("area", overlay1.getArea());
            jsonObject.put("state", overlay1.getState());
            jsonObject.put("color", overlay1.getColor());
            retdata.put("data",jsonObject);
        }else { //不是的新增
            bdMapService.saveMap(bdMap);
            BdMap overlay2 = bdMapService.findCoverByIndex(JSONObject.fromObject(Cover).getString("Cover"));
            jsonObject.put("mapCover", JSONArray.parseArray(overlay2.getCoveroord()));
            jsonObject.put("zindex", overlay2.getZindex());
            jsonObject.put("name", overlay2.getName());
            jsonObject.put("area", overlay2.getArea());
            jsonObject.put("state", overlay2.getState());
            jsonObject.put("color", overlay2.getColor());
            retdata.put("data",jsonObject);
        }


        return jsonObject.toString();
    }

    //获取所有覆盖物
    @ResponseBody
    @PostMapping(value = "getAllCover.html", produces = "application/json;charset=utf-8")
    public String getAllCover(HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        List<BdMap> mapList=bdMapService.getMapCover();
        JSONArray array = new JSONArray();

        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        for(BdMap bdMap:mapList){
            com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
            object.put("mapCover", JSONArray.parseArray(bdMap.getCoveroord()));//由于是点数组，所以转成json集合对象
            object.put("zindex", bdMap.getZindex());
            object.put("name", bdMap.getName());
            object.put("area", bdMap.getArea());
            object.put("state", bdMap.getState());
            object.put("color",  bdMap.getColor());
            array.add(object);
        }
        jsonObject.put("data",array);
        System.err.println(jsonObject); //打印
        return jsonObject.toString();
    }

    //获取一个覆盖物
    @ResponseBody
    @PostMapping(value = "getOneCover.html", produces = "application/json;charset=utf-8")
    public String getOneCover(@RequestBody String Coveroord) {
        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
        BdMap overlay = bdMapService.findCoverByIndex(JSONObject.fromObject(Coveroord).getString("Cover"));
        //用alibaba的JSON,避免数字字符串失真
        object.put("mapCover", JSONArray.parseArray(overlay.getCoveroord()));
        object.put("zindex", overlay.getZindex());
        object.put("name", overlay.getName());
        object.put("color", overlay.getColor());

        return object.toString();
    }



    //删除一个覆盖物
    @ResponseBody
    @PostMapping(value = "deleOneCover.html", produces = "application/json;charset=utf-8")
    public String deleOneCover(@RequestBody String Coveroord, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");

        bdMapService.deleCoverByIndex(JSONObject.fromObject(Coveroord).getString("Coveroord"));
        return "ok";
    }

    //修改一个覆盖物
    @ResponseBody
    @PostMapping(value = "editCover.html", produces = "application/json;charset=utf-8")
    public String editCover(@RequestBody String Coveroord, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
       // bdMapService.deleCoverByIndex(JSONObject.fromObject(Coveroord).getString("Coveroord"));
        return "ok";
    }




}
